<template>
  <div id="groupwork">  

      <el-form ref="form"  label-width="80px">           
           <el-row> 
            <el-col :span="12" >
                <el-form-item label="" label-width="0px" >
                 <el-date-picker type="date" @change="data_change" placeholder="选择日期" format="yyyy-MM-dd" value-format="yyyy-MM-dd"  v-model="time_form.time" style="width:160px;"></el-date-picker>
                </el-form-item>
            </el-col>     
            <el-col :span="12" style="text-align: right;">
                <el-button class="custom_color_button" type="primary" @click="user_but()">用户排名</el-button>
               <el-button class="custom_color_button" type="primary"  @click="grant_but()">发放前三奖励</el-button>
               <el-button class="custom_color_button" type="primary"  @click="add_but()">设置</el-button>
            </el-col>
          </el-row>
    </el-form>

   <!-- 渲染数据list -->
   <group-work-list :datalist="datalist" @up_list_fun="list_fun"> </group-work-list>

      
   <div style="margin-top:20px;text-align: center;">
      <el-pagination
        background
        @current-change="handleCurrentChange"
        :page-size="10"  
        layout="prev, pager, next, jumper"
        :total="count">
      </el-pagination>
    </div>

     <!-- 修改设置 -->
    <el-dialog title="修改设置" :visible.sync="add_mtk"  width="600px">
        <div style="width:100%;"> 

           <el-form ref="form"   label-width="70px" style="width:100%;">
            
                   <el-row>
                    <el-col :span="12">
                        <el-form-item label="成团需求">
                                <el-select  v-model="limits_form.select_num1" multiple placeholder="庄园等级" style="width:100%;">
                                    <el-option  v-for="(item,index) in limits_form.select_data" :key="index" :label="item.title" :value="item.maid"></el-option>
                                </el-select>
                          </el-form-item>     
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="邀请需求">
                                <el-select  v-model="limits_form.select_num2" multiple  placeholder="庄园等级" style="width:100%;">
                                    <el-option  v-for="(item,index) in limits_form.select_data" :key="index" :label="item.title" :value="item.maid"></el-option>
                                </el-select>
                          </el-form-item>                                  
                    </el-col>
                </el-row>

                  <el-row>
                    <el-col :span="12">
                        <el-form-item label="开始时间">
                                 <el-time-picker placeholder="选择时间"   format="HH:mm" value-format="HH:mm" v-model="add_form.time1" style="width: 100%;"></el-time-picker>
                          </el-form-item>     
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="结束时间">
                                <el-time-picker placeholder="选择时间"   format="HH:mm" value-format="HH:mm" v-model="add_form.time2" style="width: 100%;"></el-time-picker>
                          </el-form-item>                                  
                    </el-col>
                </el-row>

                  <el-row>
                    <el-col :span="12">
                        <el-form-item label="参团次数">
                            <el-input v-model.number="add_form.times" placeholder="会员参团次数" ></el-input>
                        </el-form-item>                                  
                    </el-col>
                     <el-col :span="12">
                         <el-form-item label="是否开启">
                                <el-select  v-model="limits_form.select_num3"   placeholder="是否开启" style="width:100%;">
                                    <el-option  v-for="(item,index) in limits_form.select_data3" :key="index" :label="item.name" :value="item.id"></el-option>
                                </el-select>
                          </el-form-item>                                      
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :span="12">
                        <el-form-item label="团长奖励">
                            <el-input v-model.number="add_form.tz" placeholder="团长奖励" ></el-input>
                        </el-form-item>       
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="团员奖励">
                            <el-input v-model.number="add_form.ty" placeholder="团员奖励" ></el-input>
                        </el-form-item>                                  
                    </el-col>
                </el-row>

                 <el-row>
                    <el-col :span="12">
                        <el-form-item label="第一团长">
                            <el-input v-model.number="add_form.num1_1" placeholder="输入奖励" ></el-input>
                        </el-form-item>       
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="第一团员">
                            <el-input v-model.number="add_form.num1_2" placeholder="输入奖励" ></el-input>
                        </el-form-item>                                  
                    </el-col>
                </el-row>

                 <el-row>
                    <el-col :span="12">
                        <el-form-item label="第二团长">
                            <el-input v-model.number="add_form.num2_1" placeholder="输入奖励" ></el-input>
                        </el-form-item>       
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="第二团员">
                            <el-input v-model.number="add_form.num2_2" placeholder="输入奖励" ></el-input>
                        </el-form-item>                                  
                    </el-col>
                </el-row>

                 <el-row>
                    <el-col :span="12">
                        <el-form-item label="第三团长">
                            <el-input v-model.number="add_form.num3_1" placeholder="输入奖励" ></el-input>
                        </el-form-item>       
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="第三团员">
                            <el-input v-model.number="add_form.num3_2" placeholder="输入奖励" ></el-input>
                        </el-form-item>                                  
                    </el-col>
                </el-row>

                 <el-row>
                    <el-col :span="12">
                        <el-form-item label="拼团人数">
                                <el-select  v-model="limits_form.pt_select_num" multiple placeholder="拼团人数" style="width:100%;">
                                    <el-option  v-for="(item,index) in limits_form.py_select_data" :key="index" :label="item.name" :value="item.id"></el-option>
                                </el-select>
                          </el-form-item>     
                    </el-col>
                </el-row>

              

            </el-form>

            <div style="text-align: center;width:50%;margin:auto;">
                <el-button type="primary" class="custom_color_button" style="width:80%;" @click="qr_add_but()">确认</el-button>
            </div>

        </div> 
    </el-dialog>




       <!-- 用户排名 -->
           <el-dialog title="用户排名" :visible.sync="table_mtk"  width="50%">
              <div style="width:100%;"> 
                <el-table :data="table_data" style="width: 100%" height="580" :header-cell-style="{background:'#4F81BC',color:'white'}"
                       :row-class-name="tableRowClassName"  :border="true">             
                <el-table-column label="头像"  align='center' >                   
                    <template slot-scope="scope">                   
                        <span ><img style="width: 30px; height: 30px" :src="urlimg + scope.row.head_img" /></span>
                    </template>
                </el-table-column>
                 <el-table-column label="姓名"  align='center' >                   
                    <template slot-scope="scope">                   
                        <span >{{ scope.row.username }}</span>
                    </template>
                </el-table-column>
                 <el-table-column label="开关"  align='center' >                   
                    <template slot-scope="scope">                   
                           <el-switch  
                            :active-value="active" 
                            :inactive-value="isactive" 
                            v-model="scope.row.hide" 
                            @change="switchchange(scope.row)"  
                            active-color="#F5CF49" 
                            inactive-color="#E5E5E5"> 
                         </el-switch> 
                    </template>
                </el-table-column>
                <el-table-column label="修改"  align='center' >                   
                    <template slot-scope="scope">                   
                          <el-button size="mini" type="primary"  plain @click="user_xg_but(scope.row)">修改</el-button>    
                    </template>
                </el-table-column>
               
               </el-table>  
            
                </div> 
         </el-dialog>

           <!-- 修改用户-->
    <el-dialog title="修改用户" :visible.sync="table_xg_mtk"  width="600px">
        <div style="width:100%;"> 


        <el-form ref="form"   label-width="60px" style="width:100%;">
              <el-form-item label="姓名">
                    <el-input    v-model="table_xg_form.username" placeholder="姓名"></el-input>
              </el-form-item>

              <el-row style="width:100%;">
                     <el-col :span="12">
                        <el-form-item label="头像">
                            <el-upload 
                                  action="uploadUrl"  
                                  :show-file-list="false"   
                                  :before-upload="beforeupload"
                                  :http-request="ImgUploadSectionFile">
                                  <div class="avatar-uploader">
                                      <img v-if="pic_form.head_img"  :src="pic_form.head_img" class="avatar">
                                      <i v-else class="el-icon-plus avatar-uploader-icon"></i>
                                  </div>
                            </el-upload>
                         </el-form-item>       
                     </el-col>
                    </el-row>                           
        </el-form>

            <div style="text-align: center;width:50%;margin:auto;">
                <el-button type="primary" class="custom_color_button" style="width:80%;" @click="user_xg_but_qr">确认</el-button>
            </div>

        </div> 
    </el-dialog>


    

      
  </div>
</template>

<script>
import GroupWorkList from '@/view/Activity/GroupWorkList'
export default {
  name: 'groupwork',
  data () {
    return {
        datalist:[],
        page:1,
        count:0,
        add_mtk:false,
        time_form:{
            time:''
        },
        add_form:{
           tz:'',
           ty:'',
           times:'',
           num1_1:'',
           num1_2:'',
           num2_1:'',
           num2_2:'',
           num3_1:'',
           num3_2:'',
           time1:'',
           time2:'',
           isswitch:0,
        },
        limits_form:{
            select_data:[],
            select_num1:[],
            select_num1_a:'',
            select_num2:[],
            select_num2_a:'',
            select_data3:[{"id":0,"name":"关闭"},{"id":1,"name":"开启"}],
            select_num3:'',
            pt_select_num:[],
            py_select_data:[{"name":'人数2',"id":'2'},{"name":'人数3',"id":'3'},{"name":'人数4',"id":'4'},{"name":'人数5',"id":'5'},{"name":'人数6',"id":'6'},{"name":'人数7',"id":'7'}
            ,{"name":'人数8',"id":'8'},{"name":'人数9',"id":'9'},{"name":'人数10',"id":'10'}]
        },
        arr:[],

        table_mtk:false,
        table_data:[{"name":"111"}],
        table_xg_mtk:false,
        table_xg_form:{
            username:'',
        },
        pic_form:{
           head_img:'',
           upfile:'',
        },
        my_user_id:'',
        active:0,
        isactive:1,

    }
  },
 components:{
    GroupWorkList
  },
  created(){
      this.time_form.time = this.getNowDate1()
      this.list_fun()
      this.gz_list()
      this.pz_list()
  },
  mounted (){
    
  },
  methods: {
     gz_list(){
        this.post("/manor/listOfData", {
             
	        }).then(res => {
                 this.limits_form.select_data = res.result.list
                 //console.log(res.result.list)
                
         })
    },
     pz_list(){
        this.post("/group_buy/listOfSetting", {
             
	        }).then(res => {
                 this.arr = res.result
                 //console.log(this.arr,"ssss")

                 for(let item of this.arr) {
                    if(item.variable == 'INVITEE_MANOR_LIMIT'){ //邀请要求
                       //this.limits_form.select_num2 = item.value 
                       this.limits_form.select_num2 = item.value.split(",")
                    }else if(item.variable == 'LAUNCH_MANOR_LIMIT'){ //成团要求
                       this.limits_form.select_num1 = item.value.split(",")
                    }else if(item.variable == 'REWARD_LEADER'){ //团长奖励
                       this.add_form.tz = item.value
                    }else if(item.variable == 'REWARD_MEMBER'){ //团员奖励
                       this.add_form.ty = item.value
                    }else if(item.variable == 'SPECIAL_REWARD_LEADER_FIRST'){ //第一团长
                       this.add_form.num1_1 = item.value
                    }else if(item.variable == 'SPECIAL_REWARD_LEADER_SECOND'){ //第二团长
                       this.add_form.num2_1 = item.value
                    }else if(item.variable == 'SPECIAL_REWARD_LEADER_THREE'){ //第三团长
                      this.add_form.num3_1 = item.value
                    }else if(item.variable == 'SPECIAL_REWARD_MEMBER_FIRST'){ //第一团员
                       this.add_form.num1_2 = item.value
                    }else if(item.variable == 'SPECIAL_REWARD_MEMBER_SECOND'){ //第二团员
                       this.add_form.num2_2 = item.value
                    }else if(item.variable == 'SPECIAL_REWARD_MEMBER_THREE'){ //第三团员
                       this.add_form.num3_2 = item.value
                    }else if(item.variable == 'TAKE_GROUP_NUMBER_WEEKLY'){ //次数
                       this.add_form.times = item.value
                    }else if(item.variable == 'TAKE_GROUP_START_SECOND'){ //次数
                       this.add_form.time1 = item.value
                    }else if(item.variable == 'TAKE_GROUP_END_SCCOND'){ //次数
                       this.add_form.time2 = item.value
                    }else if(item.variable == 'SWITCH_ON_OFF'){ //是否开启
                       this.limits_form.select_num3 = parseInt(item.value)
                    }else if(item.variable == 'INITIATING_GROUP_NUMBER'){ //拼团人数
                       this.limits_form.pt_select_num = item.value.split(",")
                    }

                 }
                
         })
    },
    list_fun(){
        this.post("/group_buy/listOfData", {
                page:this.page,
                onetime:this.time_form.time
	         }).then(res => {
            this.count = res.result.count
            this.datalist = res.result.list
            //console.log(this.datalist,"sdasd")
          })
    },
    add_but(){
         this.add_mtk = true
         this.pz_list()
    },
    qr_add_but(){

        let start_time = this.getTime('00:00')
        let end_time = this.getTime('13:00')
        let select_time1 = this.getTime(this.add_form.time1)
        let select_time2 = this.getTime(this.add_form.time2)
        if( select_time1 > end_time ){
             this.open1('时间范围应该在00：00-13：00','warning')
             return false
        }
        if( select_time2 > end_time ){
             this.open1('时间范围应该在00：00-13：00','warning')
             return false
        }
        if( select_time1 > select_time2 ){
             this.open1('开始时间不能大于结束时间','warning')
             return false
        }
       
       
       this.limits_form.select_num1_a = ''
       this.limits_form.select_num2_a = ''
       this.limits_form.pt_select_num_a = ''
       
       for(let i in this.limits_form.select_num1) {  
             let lenght = this.limits_form.select_num1.length
             if(i == lenght-1){
                  this.limits_form.select_num1_a = this.limits_form.select_num1_a + this.limits_form.select_num1[i]
             }else{
                 this.limits_form.select_num1_a = this.limits_form.select_num1_a + this.limits_form.select_num1[i] + ','
             }
          } 


          for(let i in this.limits_form.select_num2) {  
             let lenght = this.limits_form.select_num2.length
             if(i == lenght-1){
                  this.limits_form.select_num2_a = this.limits_form.select_num2_a + this.limits_form.select_num2[i]
             }else{
                 this.limits_form.select_num2_a = this.limits_form.select_num2_a + this.limits_form.select_num2[i] + ','
             }
          } 

          for(let i in this.limits_form.pt_select_num) {  
             let lenght = this.limits_form.pt_select_num.length
             if(i == lenght-1){
                  this.limits_form.pt_select_num_a = this.limits_form.pt_select_num_a + this.limits_form.pt_select_num[i]
             }else{
                 this.limits_form.pt_select_num_a = this.limits_form.pt_select_num_a + this.limits_form.pt_select_num[i] + ','
             }
          } 


       console.log(this.limits_form.pt_select_num_a)
    
        
        this.arr = [
                    {name: "INVITEE_MANOR_LIMIT", val: this.limits_form.select_num2_a},
                    {name: "LAUNCH_MANOR_LIMIT", val: this.limits_form.select_num1_a},
                    {name: "REWARD_LEADER", val: this.add_form.tz},
                    {name: "REWARD_MEMBER", val: this.add_form.ty},
                    {name: "SPECIAL_REWARD_LEADER_FIRST", val: this.add_form.num1_1},
                    {name: "SPECIAL_REWARD_LEADER_SECOND", val: this.add_form.num2_1},
                    {name: "SPECIAL_REWARD_LEADER_THREE", val: this.add_form.num3_1},
                    {name: "SPECIAL_REWARD_MEMBER_FIRST", val: this.add_form.num1_2},
                    {name: "SPECIAL_REWARD_MEMBER_SECOND", val: this.add_form.num2_2},
                    {name: "SPECIAL_REWARD_MEMBER_THREE", val: this.add_form.num3_2},
                    {name: "TAKE_GROUP_NUMBER_WEEKLY", val: this.add_form.times},
                    {name: "TAKE_GROUP_START_SECOND", val: this.add_form.time1},
                    {name: "TAKE_GROUP_END_SCCOND", val: this.add_form.time2},
                    {name: "SWITCH_ON_OFF", val: this.limits_form.select_num3},
                    {name: "INITIATING_GROUP_NUMBER", val: this.limits_form.pt_select_num_a}
                    ]
        this.post("/group_buy/updateSetting", {
                 arr:this.arr
                }).then(res => {
                    if(res.error_code == 0){
                    this.open1(res.result,'success')  //提示框
                    this.add_mtk = false
                     this.arr = []
                    this.pz_list()
                    }else {
                     this.arr = []
                     this.open1(res.message,'warning')
                }

        })
    },
    grant_but(){
        this.$confirm('是否发放前三奖励, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
         }).then(() => {
            this.post("/group_buy/sendSpecialReward", {
                onetime:this.time_form.time
                }).then(res => {
                    if(res.error_code == 0){
                    this.open1(res.result,'success')  //提示框
                    this.list_fun()
                    }else {
                    this.open1(res.message,'warning')
                }

             })
        }).catch(() => {
            
        });
    },
    data_change(){
        this.page = 1
        this.list_fun()
    },

    user_fun(){
        this.post("/group_virtual/listOfUser", {
               
          }).then(res => {
              this.table_data = res.result
             //console.log(res.result)
          })
    },
    user_but(){
       this.table_mtk = true
       this.user_fun()
    },
    user_xg_but(row){
       this.my_user_id = row.id
       this.pic_form.upfile = row.head_img
       this.pic_form.head_img = this.urlimg + row.head_img
       this.table_xg_form.username = row.username
       this.table_xg_mtk = true
    },
    user_xg_but_qr(){
       this.post("/group_virtual/update", {
                    id:this.my_user_id,
                    username:this.table_xg_form.username,
                    head_img:this.pic_form.upfile
                }).then(res => {
                    if(res.error_code == 0){
                    this.open1(res.result,'success')  //提示框
                    this.user_fun()
                    this.table_xg_mtk = false
                    }else {
                    this.open1(res.message,'warning')
                }

             })
    },
     switchchange(row){
          this.$confirm('是否开启/关闭, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
         }).then(() => {
                this.post("/group_virtual/showAndHide", {
                    id:row.id,
                }).then(res => {
                    if(res.error_code == 0){
                    this.open1(res.result,'success')  //提示框
                     this.user_fun()
                    }else {
                    this.open1(res.message,'warning')
                }

             })
        }).catch(() => {
                this.user_fun()
        });
    },
    beforeupload(file){
            if(this.upimglx(file) == false){  //判断是否为fales
              this.upimglxtitle()  //执行弹出错误             
              return false
            }  
            var windowURL = window.URL || window.webkitURL;               
            this.pic_form.head_img=windowURL.createObjectURL(file);             
            
            this.upimgpost("/image/uploadMultiple",file).then(res => {
              //console.log(res.result.url )
               this.pic_form.upfile = res.result.url    
            })

            return false;
     },
     ImgUploadSectionFile(param){//图片上传    
      },
     handleSizeChange(val) {
        console.log(`每页 ${val} 条`);
      },
      handleCurrentChange(val) {
        this.page = val
        this.list_fun()
        console.log(`当前页: ${val}`);
      }
  }
}
</script>


<style scoped>

[v-cloak]{
    display: none;
}
.demo{
    overflow-y:auto;
}
.mytitle{
    height: 35px;
}
.mytitle_item{
    display: flex;
    align-items: center;
}
.mytitle_item::before{
  content:'';
  width: 5px;
  height: 15px;
  background: #F5CF49;
  display: block;
}
.tb_class{
    color:#F5CF49;
    font-size:30px;
    margin-top:5px;
    margin-left:15px;
    margin-right:15px;
  }
  .my_input>>>input{
      width: 300px;
  }

  .avatar-uploader  {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 150px;
    height: 150px;
    line-height: 150px;
    text-align: center;
  }
  .avatar {
    width: 150px;
    height: 150px;
    display: block;
  }
</style>
